package com.ocean.service.library.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocean.dao.library.RecordDao;
import com.ocean.dto.RecordDto;
import com.ocean.dto.RecordEchartsDto;
import com.ocean.entity.library.Record;
import com.ocean.service.library.RecordService;
import com.ocean.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Project：SchoolManage
 * Package：com.ocean.service.library.impl
 * Author:zy
 * Time:2023/3/3
 */

@Service
public class RecordServiceImpl implements RecordService {
    @Autowired
    private RecordDao recordDao;

    @Override
    public R save(Record record) {
        if (recordDao.insert(record) > 0) {
            return R.ok();
        } else {
            return R.fail("插入借阅记录失败");
        }
    }

    @Override
    public R del(int id) {
        if (recordDao.delete(id)>0) {
            return R.ok();
        }else {
            return R.fail("删除借阅记录失败");
        }
    }

    @Override
    public R update(Record record) {
        if (recordDao.update(record)>0) {
            return R.ok();
        }else {
            return R.fail("更新借阅记录失败");
        }
    }

    @Override
    public R queryPage(int page, int size) {
        PageHelper.startPage(page, size);
        PageInfo<Record> pageInfo = new PageInfo<>(recordDao.selectAll());
        return R.ok(pageInfo);
    }

    /**
     * 文件导入
     * @param file
     * @return
     */
    @Override
    public R importData(MultipartFile file) {
        if (!file.isEmpty()){
            try {
                List<Record> list = EasyExcel.read(file.getInputStream(), Record.class, new ReadListener() {
                    @Override
                    public void invoke(Object o, AnalysisContext analysisContext) {
                    }

                    @Override
                    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    }
                }).doReadAllSync();

                if (recordDao.insertBatch(list)>0) {
                    return R.ok();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return R.fail();
    }

    @Override
    public void exportData(HttpServletResponse response) {
        //1.设置下载的响应消息头
        response.setHeader("Content-Disposition", "attachment;filename=record_" + System.currentTimeMillis()+".xlsx");
        //2.查询数据
        List<RecordDto> list=recordDao.select();
        //3.生成Excel 同时写出
        try {
            EasyExcel.write(response.getOutputStream(),RecordDto.class).sheet("自动导出").doWrite(list);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public R queryEchartsStu() {
        //1.查询数据
        List<RecordEchartsDto> list=recordDao.selectsNum();
        //2.组装数据
        Map<String, ArrayList> map=new HashMap<>();
        map.put("name",new ArrayList());
        map.put("num",new ArrayList());
        list.forEach(d->{
            map.get("name").add(d.getName());
            map.get("num").add(d.getNum());
        });
        return R.ok(map);
    }

    @Override
    public R queryEchartsBook() {
        //1.查询数据
        List<RecordEchartsDto> list=recordDao.selectbNum();
        //2.组装数据
        Map<String, ArrayList> map=new HashMap<>();
        map.put("name",new ArrayList());
        map.put("num",new ArrayList());
        list.forEach(d->{
            map.get("name").add(d.getName());
            map.get("num").add(d.getNum());
        });
        return R.ok(map);
    }
}
